package inet.app.model.util;

import inet.app.model.object.Csd_Item;
import inet.app.model.object.Lbt_Item;
import inet.app.model.object.LoXq_Item;
import inet.app.model.object.Lx_Item;
import inet.app.model.object.Member;
import inet.app.model.object.Sc_Item;

import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.text.TextUtils;

public class MyPreferenceManager {

	public final static String SOICAU = "SC";
	public final static String CAU_SO_DEP = "CSD";
	public final static String LO_BACH_THU = "LBT";
	public final static String LO_XIEN = "LX";
	public final static String LO_XIEN_QUAY = "LXQ";
	public final static String LO_AM_DUONG = "LAD";
	public final static String L0_NGU_HANH = "LNH";
	public final static String DAN_DAC_BIET_NGAY = "DDBN";
	public final static String DAN_DAC_BIET_TUAN = "DDBT";

	public final static String TKDB_DAU = "TKDB_DAU";
	public final static String TKDB_DUOI = "TKDB_DUOI";
	public final static String TKDB_TONG = "TKDB_TONG";
	public final static String TKDB_BO = "TKDB_BO";
	public final static String IS_FREE = "is_free";

	public final static String MEM_ID = "mem_id";
	public final static String MEM_FULLNAME = "mem_fullname";
	public final static String MEM_EMAIL = "mem_email";
	public final static String MEM_USERNAME = "mem_username";
	public final static String MEM_MOBILE = "mem_mobile";
	public final static String MEM_GENDER = "mem_gender";
	public final static String MEM_BIRTHDAY = "mem_birthday";

	private static MyPreferenceManager instance;
	private MyPreference preference;

	private MyPreferenceManager(Context context) {
		this.preference = MyPreference.getInstance(context);
	}

	public static MyPreferenceManager getInstance(Context context) {
		if (instance == null) {
			instance = new MyPreferenceManager(context);
		}
		return instance;
	}

	/**
	 * clear values
	 */
	public void clear() {
		preference.clear();
	}

	public void setFree(boolean isFree) {
		preference.putBoolean(IS_FREE, isFree);
	}

	public boolean isFree() {
		return preference.getBoolean(IS_FREE, true);
	}

	public void setSoiCau(String code, Sc_Item item) {
		String rootKey = new StringBuffer(SOICAU).append("_").append(code)
				.toString();
		preference
				.putString(rootKey.concat("_").concat("code"), item.getCode());
		preference
				.putString(rootKey.concat("_").concat("name"), item.getDate());
		preference
				.putString(rootKey.concat("_").concat("date"), item.getDate());
		preference.putString(rootKey.concat("_").concat("decham"),
				item.getDecham());
		preference.putString(rootKey.concat("_").concat("dudoande"),
				item.getDudoande());
		preference.putString(rootKey.concat("_").concat("dudoanlo"),
				item.getDudoanlo());
		preference.putString(rootKey.concat("_").concat("loto3so"),
				item.getLoto3so());
		preference.putString(rootKey.concat("_").concat("luachon"),
				item.getLuachon());
	}

	public Sc_Item getSoiCau(String code) {
		Sc_Item item = new Sc_Item();
		String rootKey = new StringBuffer(SOICAU).append("_").append(code)
				.toString();
		item.setCode(preference.getString(rootKey.concat("_").concat("code"),
				""));
		item.setName(preference.getString(rootKey.concat("_").concat("name"),
				""));
		item.setDate(preference.getString(rootKey.concat("_").concat("date"),
				""));
		item.setDecham(preference.getString(rootKey.concat("_")
				.concat("decham"), ""));
		item.setName(preference.getString(rootKey.concat("_").concat("name"),
				""));
		item.setDudoande(preference.getString(
				rootKey.concat("_").concat("dudoande"), ""));
		item.setDudoanlo(preference.getString(
				rootKey.concat("_").concat("dudoanlo"), ""));
		item.setLuachon(preference.getString(
				rootKey.concat("_").concat("luachon"), ""));
		return (TextUtils.isEmpty(item.getCode())) ? null : item;
	}

	public void setSoiCauSoDep(String code, Csd_Item item) {
		String rootKey = new StringBuffer(CAU_SO_DEP).append("_").append(code)
				.toString();
		preference
				.putString(rootKey.concat("_").concat("code"), item.getCode());
		preference
				.putString(rootKey.concat("_").concat("name"), item.getName());
		preference
				.putString(rootKey.concat("_").concat("date"), item.getDate());
		preference.putString(rootKey.concat("_").concat("cau10"),
				item.getCau10());
		preference
				.putString(rootKey.concat("_").concat("cau9"), item.getCau9());
		preference
				.putString(rootKey.concat("_").concat("cau8"), item.getCau8());
		preference
				.putString(rootKey.concat("_").concat("cau7"), item.getCau7());
		preference.putString(rootKey.concat("_").concat("luachon"),
				item.getLuachon());
	}

	public Csd_Item getSoiCauSoDep(String code) {
		Csd_Item item = new Csd_Item();
		String rootKey = new StringBuffer(CAU_SO_DEP).append("_").append(code)
				.toString();
		item.setCode(preference.getString(rootKey.concat("_").concat("code"),
				""));
		item.setName(preference.getString(rootKey.concat("_").concat("name"),
				""));
		item.setDate(preference.getString(rootKey.concat("_").concat("date"),
				""));
		item.setCau10(preference.getString(rootKey.concat("_").concat("cau10"),
				""));
		item.setCau9(preference.getString(rootKey.concat("_").concat("cau9"),
				""));
		item.setCau8(preference.getString(rootKey.concat("_").concat("cau8"),
				""));
		item.setCau7(preference.getString(rootKey.concat("_").concat("cau7"),
				""));
		item.setLuachon(preference.getString(
				rootKey.concat("_").concat("luachon"), ""));
		return (TextUtils.isEmpty(item.getCode())) ? null : item;
	}

	public void setLoBachThu(String code, Lbt_Item item) {
		String rootKey = new StringBuffer(LO_BACH_THU).append("_").append(code)
				.toString();
		preference
				.putString(rootKey.concat("_").concat("code"), item.getCode());
		preference
				.putString(rootKey.concat("_").concat("name"), item.getName());
		preference
				.putString(rootKey.concat("_").concat("date"), item.getDate());
		preference
				.putString(rootKey.concat("_").concat("most"), item.getMost());
		preference.putString(rootKey.concat("_").concat("least"),
				item.getLeast());
		preference.putString(rootKey.concat("_").concat("luachon"),
				item.getLuachon());
	}

	public Lbt_Item getLoBachThu(String code) {
		Lbt_Item item = new Lbt_Item();
		String rootKey = new StringBuffer(LO_BACH_THU).append("_").append(code)
				.toString();
		item.setCode(preference.getString(rootKey.concat("_").concat("code"),
				""));
		item.setName(preference.getString(rootKey.concat("_").concat("name"),
				""));
		item.setDate(preference.getString(rootKey.concat("_").concat("date"),
				""));
		item.setMost(preference.getString(rootKey.concat("_").concat("most"),
				""));
		item.setLeast(preference.getString(rootKey.concat("_").concat("least"),
				""));
		item.setLuachon(preference.getString(
				rootKey.concat("_").concat("luachon"), ""));
		return (TextUtils.isEmpty(item.getCode())) ? null : item;
	}

	public void setLoXien(String code, Lx_Item item) {
		String rootKey = new StringBuffer(LO_XIEN).append("_").append(code)
				.toString();
		preference
				.putString(rootKey.concat("_").concat("code"), item.getCode());
		preference
				.putString(rootKey.concat("_").concat("name"), item.getName());
		preference
				.putString(rootKey.concat("_").concat("date"), item.getDate());
		preference.putString(rootKey.concat("_").concat("xien2"),
				item.getXien2());
		preference.putString(rootKey.concat("_").concat("xien3"),
				item.getXien3());
		preference.putString(rootKey.concat("_").concat("xien4"),
				item.getXien4());
	}

	public Lx_Item getLoXien(String code) {
		Lx_Item item = new Lx_Item();
		String rootKey = new StringBuffer(LO_XIEN).append("_").append(code)
				.toString();
		item.setCode(preference.getString(rootKey.concat("_").concat("code"),
				""));
		item.setName(preference.getString(rootKey.concat("_").concat("name"),
				""));
		item.setDate(preference.getString(rootKey.concat("_").concat("date"),
				""));
		item.setXien2(preference.getString(rootKey.concat("_").concat("xien2"),
				""));
		item.setXien3(preference.getString(rootKey.concat("_").concat("xien3"),
				""));
		item.setXien4(preference.getString(rootKey.concat("_").concat("xien4"),
				""));
		return (TextUtils.isEmpty(item.getCode())) ? null : item;
	}

	public void setLoXienQuay(String code, String action, LoXq_Item item) {
		String rootKey = new StringBuffer(LO_XIEN_QUAY).append("_")
				.append(code).append("_").append(action).toString();
		preference
				.putString(rootKey.concat("_").concat("code"), item.getCode());
		preference
				.putString(rootKey.concat("_").concat("name"), item.getName());
		preference
				.putString(rootKey.concat("_").concat("date"), item.getDate());
		preference
				.putString(rootKey.concat("_").concat("info"), item.getInfo());
	}

	public LoXq_Item getLoXienQuay(String code, String action) {
		LoXq_Item item = new LoXq_Item();
		String rootKey = new StringBuffer(LO_XIEN_QUAY).append("_")
				.append(code).append("_").append(action).toString();
		item.setCode(preference.getString(rootKey.concat("_").concat("code"),
				""));
		item.setName(preference.getString(rootKey.concat("_").concat("name"),
				""));
		item.setDate(preference.getString(rootKey.concat("_").concat("date"),
				""));
		item.setInfo(preference.getString(rootKey.concat("_").concat("info"),
				""));
		return (TextUtils.isEmpty(item.getCode())) ? null : item;
	}

	public void setContentTKDB(String select, String json) {
		preference.putString(select, json);
	}

	public String getContentTKDB(String select) {
		return preference.getString(select, "");
	}

	public void setLoadTKDB(String key, boolean value) {
		preference.putBoolean(key + "_first_load", value);
	}

	public boolean isLoadTKDB(String key) {
		return preference.getBoolean(key + "_first_load", false);
	}

	public void setMember(Member member) {

		preference.putLong(MEM_ID, member.getId());
		preference.putString(MEM_FULLNAME, member.getFullname());
		preference.putString(MEM_EMAIL, member.getEmail());
		preference.putString(MEM_USERNAME, member.getUsername());
		preference.putString(MEM_MOBILE, member.getMobile());
		preference.putInt(MEM_GENDER, member.getGender());
		preference.putString(MEM_BIRTHDAY, member.getBirthday());
	}

	public void clearMember() {
		preference.putLong(MEM_ID, -1);
	}

	public Member getMember() {

		long id = preference.getLong(MEM_ID, -1);
		if (id == -1) {
			return null;
		}
		Member memPref = new Member();
		memPref.setId(id);
		memPref.setFullname(preference.getString(MEM_FULLNAME, ""));
		memPref.setEmail(preference.getString(MEM_EMAIL, ""));
		memPref.setUsername(preference.getString(MEM_USERNAME, ""));
		memPref.setMobile(preference.getString(MEM_MOBILE, ""));
		memPref.setGender(preference.getInt(MEM_GENDER, 1));
		memPref.setBirthday(preference.getString(MEM_BIRTHDAY, ""));
		return memPref;
	}

	public void setPosAudio(String code, String matinh, String date, int value) {
		String key = new StringBuffer("audio_").append(code).append(matinh)
				.append(date).toString();
		preference.putInt(key, value);
	}

	public int getPosAudio(String code, String matinh, String date) {
		String key = new StringBuffer("audio_").append(code).append(matinh)
				.append(date).toString();
		return preference.getInt(key, 0);
	}
	
	/**
	 * Clear all pos audio saved
	 */
	public void clearPosAudio() {
		Map<String, ?> posAudio = preference.getAll();
		Set<String> keyAudios = posAudio.keySet();
		for (String keyAudio : keyAudios) {
			if (keyAudio.startsWith("audio")) {
				preference.putInt(keyAudio, 0);
			}
		}
	}
}
